<?php
/**
 * xushuhui
 * 2017/8/3
 * 9:58
 */
namespace app\index\controller;

use app\index\model\User;
use app\index\model\Wxuser;
use app\index\service\Sms;
use app\lib\exception\LoginException;
use app\lib\exception\RegisterException;
use app\lib\exception\SmsException;
use app\lib\exception\SuccessMessage;
use app\lib\exception\WeChatException;
use app\lib\validate\MobileValidate;
use app\lib\validate\UserLogin;
use app\lib\validate\UserRegister;
use think\Controller;
use think\Request;
use think\Url;
use Wx\Wechat;

class Login extends Controller
{
	//登录页面
	public function index()
	{
		$data = Request::instance()->post();
		if ($data)
		{
			(new UserLogin())->goCheck();
			$userData = User::where('mobile','=',$data['mobile'])->with(['wxuser'])->find();
			if(!$userData){
				throw new LoginException([
					'msg' => '没有该用户，请注册'
				]);
			}
			if($userData['password'] !== md5($data['password'])){
				throw new LoginException([
					'msg' => '账户密码不匹配，请重新输入'
				]);
			}
			session('uid',$userData['id']);
			session('mobile',$userData['mobile']);
			session('openid',$userData['wxuser']['openid']);
			throw new SuccessMessage();
		}else{

			return view();
		}
	}
	//注册页面
	public function reg()
	{
		$data = Request::instance()->post();
		if ($data) {
			(new UserRegister())->goCheck();
			if($data['smscode'] != session('regCode')){
				throw new RegisterException([
					'msg' => '短信验证码错误，请重新获取输入'
				]);
			}
			//验证激活码
			$keyId = Key::where('key','=',$data['code'])->where('is_active','=',0)->field('id')->find();
			if(!$keyId){
				throw new RegisterException([
						'msg' => '激活码错误，请联系代理商'
				]);
			}
			$res = User::create($data,true);
			if(!$res){
				throw new RegisterException([
						'msg' => '注册失败，请重试'
				]);
			}
			$uid = $res->id;
			// 激活码更新
			Key::update(['uid' => $uid,'is_active' => 1],['id'=>$keyId['id']]);
			session('uid',$uid);
			session('mobile',$data['mobile']);
			session('regCode',null);
			throw new SuccessMessage();
		}else{
			return view();
		}

	}
	//发送短信
	public function sendMsg()
	{
		(new MobileValidate())->goCheck();
		$data = Request::instance()->post();
		$res = Sms::send($data['mobile']);
		if($res['Code'] !== 'OK'){
			throw new SmsException();
		}
		session('regMobile',$data['mobile']);
		throw new SuccessMessage();
	}
	// 退出
	public function logout()
	{
		session(null);
		$this->redirect('index');
	}
	//微信登录
	public function wechat()
	{
		$app_id = config('wx.app_id');
		$app_secret = config('wx.app_secret');
		$url = Url::build('Index/Login/getUserInfo','',true,true);
		$res = Wechat::instance($app_id,$app_secret)->getBaseInfo($url);
	}
	// 微信获取用户信息
	public function getUserInfo()
	{
		$app_id = config('wx.app_id');
		$app_secret = config('wx.app_secret');
		$code = $_GET['code'];
		$data = Wechat::instance($app_id,$app_secret)->getUserInfo($code);
		if(!$data){
			$this->redirect('code');

			throw new WeChatException([
				'msg' => '未关注公众号，请搜索和诚云'
			]);
		}
		
		// 查询是否已经注册
		$userdata = Wxuser::where('openid',$data['openid'])->find();

		if($userdata){
			session('uid',$userdata['uid']);
			$this->redirect("Index/index");
		}
		$wxObj = Wxuser::create($data,true);
		if($wxObj->id == 0){
			throw new WeChatException([
				'msg' => '微信验证失败'
			]);
		}
		$userObj = User::create(['name' => $data['nickname']]);
		
		$ret = Wxuser::where('id', $wxObj->id)->update(['uid' => $userObj->id]);
		
		session("uid",$userObj->id);
		$this->redirect('Index/index');
	}
	// 忘记密码
	public function password()
	{
		return view();
	}
	//未关注二维码页面
	public function code()
	{
		return view();
	}

	
}